import store from "../store/index.js";
import {
	NestPayload
} from "./stores/nest";

// interface IncommingData {
// 	time : string;
// 	topic : string;
// 	message : string;
// }

let rcTimeout = 0;

export default {
	HandleIncomming(Idata) {
		const message = Idata.message;
		this.handleMessage(Idata.topic, message);
	},
	handleMessage(topic, message) {
		const id = topic?.split("/")[1];
		if (!id) return;
		const setMessage = (commit, key1, key2) => {
			const json = JSON.parse(message);
			if (typeof json[key1] === "undefined") return;
			const data = {}
			if (key1 === "currentTransportRate") {
				const num = json[key1].split("MBps")[0];
				json[key1] = num.substring(0, num.indexOf(".") + 3) + "MBps";
			}
			if (key2) {
				data[key2] = json;
			} else {
				data[key1] = json[key1];
			}
			store.commit(commit, {
				data,
				id
			});
		};

		if (topic?.includes("callback")) {
			console.warn("Callback ==>> !", message);
			// store.commit("waypointStatus/statusUpdate",message);
			store.dispatch("commandStatus/popCommandCode", JSON.parse(message));
			store.commit("commandStatus/popCommand", JSON.parse(message).pCode);

			const messageJson = JSON.parse(message);
			const code = messageJson.code;
			const pCode = messageJson.pCode;

			if (pCode === "10006" || pCode === "00001" || pCode === "00002" || pCode === "00003") {
				if (code !== "1001") {
					store.dispatch("middleSnackbar/openSnackbar", {
						msg: message,
						color: "red"
					});
				}
			}
			// 实现命令发送状态显示: 增加一个函数，只要callback收到code为1001的消息, 在消息发送队列中, 如果存在对应的pCode, 提示发送成功, 并删除所有pCode对应的记录,
			// 注意消息发送队列的code在callback消息里面是对应pCode
			if (code === "1001" || code == "4001") {
				const item = store.state.commandNotificationWaitQueue.find((q) => q.commandCode === pCode);
				if (item) {
					store.commit("pushCmdToNotifyQueue", item);
				}
				store.commit("deleteCmdNotifyQueueWaitByCode", {
					code: pCode
				});

				// if (pCode === "13008") {
				// 	store.commit("home/setAircraftVideoState", { id, state: true });
				// 	// 打开推流
				// } else if (pCode === "13009") {
				// 	store.commit("home/setAircraftVideoState", { id, state: false });
				// 	// 关闭
				// }
				switch (pCode) {
					case "13008": {
						store.commit("smartFlight/setAircraftVideoState", {
							id,
							state: true
						});
					}
					break;

					case "13009": {
						store.commit("smartFlight/setAircraftVideoState", {
							id,
							state: false
						});
					}
					break;

					case "10003": {
						if (code === "1001") {
							store.commit("commandStatus/updateMissionUploading", false);
						}
					}
				}
			}
		}

		if (topic?.includes("status/base")) {
			rcTimeout = new Date().getTime();
			setMessage("nest/setNestData", "nestStateConstant");
			setMessage("nest/setNestData", "isRemoteControllerConnected");
			setMessage("nest/setNestData", "isAircraftConnected");
			setMessage("nest/setNestData", "aircraftStateConstant");
		}
		if (new Date().getTime() - rcTimeout > 5000) {
			const data = {}
			data["isRemoteControllerConnected"] = false;
			store.commit("nest/setNestData", {
				data,
				id
			});
		}

		if (topic?.includes("status/aircraft_battery")) {
			setMessage("nest/setNestData", "aircraftBatteryChargeInPercent");
		}
		if (topic?.includes("status/waypoint")) {
			setMessage("nest/setNestData", "currentMissionID");
			setMessage("nest/setNestData", "execMissionID");
		}
		if (topic?.includes("status/aircraft")) {
			setMessage("nest/setNestData", "satelliteCount");
			setMessage("nest/setNestData", "aircraftDownLinkSignal");
			setMessage("nest/setNestData", "aircraftUpLinkSignal");
			setMessage("nest/setNestData", "flightMode");
			setMessage("nest/setNestData", "ultrasonicHeightInMeters");
			setMessage("nest/setNestData", "aircraftAltitude");
			setMessage("nest/setNestData", "aircraftLocationLatitude");
			setMessage("nest/setNestData", "aircraftLocationLongitude");
			setMessage("nest/setNestData", "homeLocationLatitude");
			setMessage("nest/setNestData", "homeLocationLongitude");
			setMessage("nest/setNestData", "areMotorsOn");
			setMessage("nest/setNestData", "aircraftPitch");
			setMessage("nest/setNestData", "aircraftRoll");
			setMessage("nest/setNestData", "aircraftYaw");
			setMessage("nest/setNestData", "aircraftHSpeed");
			setMessage("nest/setNestData", "aircraftVSpeed");
			setMessage("nest/setNestData", "aircraftFlyInSecond");
			setMessage("nest/setNestData", "isLiveStreaming");
		}
		if (topic?.includes("status/ac")) {
			setMessage("nest/setNestData", "insideACTemperature");
		}
		if (topic?.includes("status/ws")) {
			setMessage("nest/setNestData", "speed");
			setMessage("nest/setNestData", "rainfall");
		}
		if (topic?.includes("status/motor")) {
			setMessage("nest/setNestData", "cabinState", "motorStatus");
			setMessage("nest/setNestData", "chargeState", "motorStatus");
			setMessage("nest/setNestData", "syncState", "motorStatus");
			setMessage("nest/setNestData", "gatherStatus", "motorStatus");
		}
		if (topic?.includes("status/media")) {
			setMessage("nest/setNestData", "currentState"); //传输状态
			// setMessage("nest/setNestData", "currentTransportRate"); //传输速度
			setMessage("nest/setNestData", "totalFileNum"); //SD卡中数据总数
			setMessage("nest/setNestData", "failedNum"); //SD卡传输失败数
		}
		if (topic?.includes("status/qrcodelight")) {
			setMessage("nest/setNestData", "lightstatus");
		}
		if (topic?.includes("status/batteryerror")) {
			setMessage("nest/setNestData", "isCurrentOverloaded");
			setMessage("nest/setNestData", "isOverHeated");
			setMessage("nest/setNestData", "isLowTemperature");
			setMessage("nest/setNestData", "isShortCircuited");
			setMessage("nest/setNestData", "isCustomDischargeEnabled");
			setMessage("nest/setNestData", "lowVoltageCellIndex");
			setMessage("nest/setNestData", "damagedCellIndex");
		}
		if (topic?.includes("status/aircraft_battery")) {
			setMessage("nest/setNestData", "numberOfDischarges");
			setMessage("nest/setNestData", "aircraftBatteryCurrentTemperature");
			setMessage("nest/setNestData", "aircraftBatteryCurrentVoltage");
			setMessage("nest/setNestData", "isBatteryConnected");
		}
		if (topic?.includes("status/rc")) {
			setMessage("nest/setNestData", "remainingChargeInmAh");
			setMessage("nest/setNestData", "remainingChargeInPercent");
			setMessage("nest/setNestData", "isCharging");
			setMessage("nest/setNestData", "virtualStickState");
		}
		if (topic?.includes("status/power")) {
			setMessage("nest/setNestData", "ChargerStatus");
		}
		if (topic?.includes("status/camera")) {
			setMessage("nest/setNestData", "currentCameraMode");
		}

		if (topic?.includes("status/rtk")) {
			setMessage("nest/setNestData", "rtkHealthy");
		}
		if (topic?.includes("status/gimbal")) {
			setMessage("nest/setNestData", "gimbalPitch", "gimbal");
			setMessage("nest/setNestData", "gimbalYaw", "gimbal");
			setMessage("nest/setNestData", "gimbalRoll", "gimbal");
		}
		if (topic?.includes("status/nestrc")) {
			setMessage("nest/setNestData", "Temperature");
		}
		if (topic?.includes("status/weather")) {
			setMessage("nest/setNestData", "WindSpedd", "weather");
			setMessage("nest/setNestData", "CurrRainSnowStatu", "weather");
		}
	}
};